//查询指定url参数
 function GetQueryString(name) {
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}

  var id = GetQueryString("id");
  var obj;
  var storeId;
  $.ajax({
            url : "/app/goods/detail.api",
            type : "post",
            data:{
              "id":id
            },
            success : function(datas){
	            var html ="";
	            obj = datas.data;
	            storeId = obj.storeId;
	            var imgs = obj.imgs;
	            if(imgs!=null){
	              imgs=imgs.split(",");
	              for(i=0;i<imgs.length;i++){
	                var img = imgs[i];
	                html = html + 
	                 '<div class="swiper-slide" onclick="javascript:void(return 0;window.location.href = '+"'countDown.html'"+');">'+
	                '  <img class="img" src="'+img+'">'+
	                '</div>'
	              }
	              $(".swiper-wrapper").html(html);
	              var swiper = new Swiper('.swiper-container', {
	                pagination: '.swiper-pagination',
	                paginationClickable: true,
	                parallax: true,
	                loop: true,
	                zoom: true,
	                centeredSlides: true,
	                speed: 1000,
	                autoplay: 1000,
	              });
	            }
	            
	            $(".productName").text(obj.goodsName);
	            $(".storeDeliverFee").text(obj.dispatchingFee);
	            $(".sellNum").text(obj.sellNum);
	            $(".productSell").text(obj.salePrice);
	            $("#shopFee").text(obj.salePrice);
	            $("#shopDispatherFee").text(obj.dispatchingFee);
	            $(".content").html(obj.detailDescription);

	            $.ajax({
                    url : "/app/goods/getCommentList.api",
                    type : "post",
                    data:{
                      "goodsId":id
                    },
                    success : function(datas){
	                    var obj = datas.rows || [];
	                    if(obj==null || obj.length == 0){
	                      $(".goodsEvaluateList").hide();
	                      $(".goodsEvaluateTitle").hide();
	                    }else{
	                      var goodsEvaluate = obj[0];
	                       $(".goodsEvaluate img").attr("src", goodsEvaluate.avatar);
	                       $(".nickname").text(goodsEvaluate.nickName);
	                       $(".goodsEvaluateContent").text(goodsEvaluate.comment);
	                       $(".goodsEvaluateDate span").text(goodsEvaluate.updateTime.substring(0,10));          
	                    }
                    }
                });
            }
        });

   //购买点击事件
  $(".btnGoodsBuy").click(function(){
       if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {  
          //判断iPhone|iPad|iPod|iOS
        /* var message = {"method":"buy","args":'{"goodsId":"'+id+'"}'};
         window.webkit.messageHandlers.webViewApp.postMessage(message);*/
         window.location.href='yiyuxshop://doAction?param={"goodsId":"'+id+'","storeId":"'+storeId+'"}'
      } else if (/(Android)/i.test(navigator.userAgent)) {   //判断Android
         window.location.href='test://xshop/doAction?param={"goodsId":"'+id+'","storeId":"'+storeId+'"}'
       }
  });
  $(".goodsEvaluateSpan").click(function(){
	  $(this).addClass('active');
	  $(".goodsAttributeSpan").removeClass('active');
      $(".content").html(obj.detailDescription);
  });
  $(".goodsAttributeSpan").click(function(){
	  $(this).addClass('active');
	  $(".goodsEvaluateSpan").removeClass('active');
      $(".content").html('<div class="attribute">' + obj.attribute + '</div>');
  });
  $('.productLove').on('click', function() {
	  if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {  
         window.location.href='yiyuxshop://doCollect?param={"goodsId":"'+id+'","storeId":"'+storeId+'"}'
      } else if (/(Android)/i.test(navigator.userAgent)) {   //判断Android
         window.location.href='test://xshop/doCollect?param={"goodsId":"'+id+'","storeId":"'+storeId+'"}'
       }
  });
  $('.productShare').on('click', function() {
 	 if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {  
         window.location.href='yiyuxshop://doShare?param={"goodsId":"'+id+'","goodsName":"'+obj.goodsName+'","picture":"'+obj.picture+'"}'
      } else if (/(Android)/i.test(navigator.userAgent)) {   //判断Android
         window.location.href='test://xshop/doShare?param={"goodsId":"'+id+'","goodsName":"'+obj.goodsName+'","picture":"'+obj.picture+'"}'
       }
  });